//
//  RadioView.m
//  RainbowFM
//
//  Created by Kratos on 14/12/10.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "RadioView.h"
#import "UIImageView+Circle.h"

#import "DOUAudioStreamer.h"

static void *kStatusKVOKey = &kStatusKVOKey;

@interface RadioView ()
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *frequencyLabel;

@property (strong, nonatomic) DOUAudioStreamer *streamer;
@property (strong, nonatomic) Track *track;
@end

@implementation RadioView

-(void)awakeFromNib
{
    [_headImageView showAsCircle];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.52fuqing.com/upload/news/2014-5-29/20145292019353785if0b.jpg"]];
}

-(void)setRadioTrack:(Track *)track
{
    _track = track;
    
    _track = [Track new];
    _track.audioFileURL = [NSURL URLWithString:@"http://mr4.douban.com/201412102236/3045e6b693515bf3cbcf83420031b235/view/song/small/p628723.mp4"];
    
    [self resetStreamer];
}


- (void)cancelStreamer
{
    if (_streamer != nil) {
        [_streamer pause];
        [_streamer removeObserver:self forKeyPath:@"status"];
        _streamer = nil;
    }
}

- (void)resetStreamer
{
    [self cancelStreamer];
    
    if (_track)
    {
        _streamer = [DOUAudioStreamer streamerWithAudioFile:_track];
        [_streamer addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:kStatusKVOKey];
        
        [_streamer play];
    }
    
}

- (void)updateStatus
{
    switch ([_streamer status]) {
        case DOUAudioStreamerPlaying:
            
            break;
            
        case DOUAudioStreamerPaused:
            
            break;
            
        case DOUAudioStreamerIdle:
            
            break;
            
        case DOUAudioStreamerFinished:
            
            break;
            
        case DOUAudioStreamerBuffering:

            break;
            
        case DOUAudioStreamerError:

            break;
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == kStatusKVOKey) {
        [self performSelector:@selector(updateStatus)
                     onThread:[NSThread mainThread]
                   withObject:nil
                waitUntilDone:NO];
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

- (void)actionPlayPause:(id)sender
{
    if ([_streamer status] == DOUAudioStreamerPaused ||
        [_streamer status] == DOUAudioStreamerIdle) {
        [_streamer play];
    }
    else {
        [_streamer pause];
    }
}

-(IBAction)takeoffFM:(id)sender
{
    if ([_delegate respondsToSelector:@selector(canceledStreamer:)]) {
        [_delegate canceledStreamer:self];
    }
}

-(void)play
{
    if (!_streamer) {
        return;
    }
    [_streamer play];
}

-(void)pause
{
    if (!_streamer) {
        return;
    }
    [_streamer pause];
}

-(void)dealloc
{
    [self cancelStreamer];
}
@end
